package com.lovo.psc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.psc.dto.OrderDto;
import com.lovo.psc.entity.PscCommodity;
import com.lovo.psc.entity.PscOrder;
import com.lovo.psc.entity.PscTriumph;
import com.lovo.psc.mapper.PscOrderMapper;
import com.lovo.psc.service.IPscCommodityService;
import com.lovo.psc.service.IPscOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.util.MQUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-07-07
 */
@Service
public class PscOrderServiceImpl extends ServiceImpl<PscOrderMapper, PscOrder> implements IPscOrderService {
    @Autowired
    IPscCommodityService service;

    @Autowired
    RabbitTemplate rabbitTemplate;


    @Override
    public List<OrderDto> findIdOrder(Integer supplyCode) {
        return baseMapper.findIdOrder(supplyCode);
    }

    @Override
    public List<OrderDto> findIdByStateOrder(Integer supplyCode, String supplyState) {
        return baseMapper.findIdByStateOrder(supplyCode,supplyState);
    }

    @Override
    public String AddOrder(PscTriumph pscTriumph) {
        if (pscTriumph!=null){
            OrderDto orderDto = new OrderDto();
            orderDto.setFkUserOrderId(pscTriumph.getFkSuppliernumber().toString());
            orderDto.setCommodityName(pscTriumph.getCommodityname());
            orderDto.setCommodityNum(pscTriumph.getCommoditynum());
            orderDto.setCommoditySize(pscTriumph.getCommoditysize());
            orderDto.setCommoditySort(pscTriumph.getCommoditysort());
            orderDto.setCommodityPrice(pscTriumph.getPrice());
            orderDto.setShippingAddress("驷马桥四路");
            orderDto.setState("0");
            orderDto.setCollectAddress("前锋路三号");
            orderDto.setBatch(orderDto.getOrderNumber()+"批次");
            orderDto.setSupplierName(pscTriumph.getSuppliername());
            rabbitTemplate.convertAndSend(MQUtil.directExchangeSupplyOrder,MQUtil.SupplyOrderKey,orderDto);
            rabbitTemplate.convertAndSend(MQUtil.directExchangeSupplyOrderCsc,MQUtil.SupplyOrderCscKey,orderDto);
            baseMapper.AddOrder(orderDto);
            QueryWrapper<PscCommodity> qw = new QueryWrapper<>();
            qw.eq("fk_commodityMumber",pscTriumph.getFkSuppliernumber());
            qw.eq("NAME",pscTriumph.getCommodityname());
            PscCommodity commodity = service.getOne(qw);
            commodity.setEum(commodity.getEum()-pscTriumph.getCommoditynum());
            service.updateById(commodity);
            return "ok";
        }
        return "null";
    }

    @Override
    public void DelOrder(String delId) {
        baseMapper.DelOrder(delId);
    }
}
